// =============================================================================
//  STATS - Statistical Analysis Tools, v.0.01
//  Copyright (C) 2011  Marco Vettigli
// -----------------------------------------------------------------------------
//
// This program  is free software:  you can  redistribute it and/or  modify it
// under the terms of the  GNU General Public License as published by the Free
// Software Foundation,  either version 3 of the License,  or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY;  without even  the  implied  warranty  of  MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License  for
// more details.
//
// You  should  have received a copy of the  GNU General Public License  along
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// == module ===================================================================
//
// SROWPROPERTY.CPP
// The class defines a properties of a given row inside STable.
//
// Author: Marco Vettigli
//
// This class is defined to store information (properties) of a specific row
// to be used for STable row management. This properties are collectively
// applied to all values (from different columns) in the row for data analysis
// and visualization. Specific properties are marker type , color, width, size
// for visualization and selection state for table selection management.
//
// == implementation ===========================================================
//
// All properties are stored as private variable and content accessed by
// getter/setter methods. Marker types are defined in the enum RowMarker.
// The function getPixmap() return a 16x16 pixmap of the marker associated to
// the row with all defined properties for data visualization.
//
// =============================================================================

// == include directives =======================================================

#include <QPainter>
#include <QPen>
#include <QDebug>
#include "srowproperty.h"

// == constructors and destructors =============================================

StatS::SRowProperty::SRowProperty()
{
    // initialize object properties
    _marker = Dot;
    _selectedState = false;
    _color = Qt::black;
    _width = 2;
    _size = 3;
}

// == getters ==================================================================

StatS::SRowProperty::RowMarker StatS::SRowProperty::marker() const
{
    return _marker;
}

int StatS::SRowProperty::size() const
{
    return _size;
}

int StatS::SRowProperty::width() const
{
    return _width;
}

QColor StatS::SRowProperty::color() const
{
    return _color;
}

// == setters ==================================================================

bool StatS::SRowProperty::setMarker(StatS::SRowProperty::RowMarker marker)
{
    // check consistency of marker
    switch (marker) {
    case Dot:
    case Circle:
    case Square:
    case Triangle:
        break;
    default:
        qWarning() << "RowMarker not defined: " << _marker;
        return false;
    }
    // check if new marker is different from current
    if (_marker == marker) return false;
    // assign marker and return
    _marker = marker;
    return true;
}

bool StatS::SRowProperty::setSelected(bool state)
{
    // check if new state is different from current
    if (_selectedState == state) return false;
    // assign new state and return
    _selectedState = state;
    _width = 2 + state;
    return true;
}

bool StatS::SRowProperty::setColor(QColor color)
{
    // check if new color is different from current
    if (_color == color) return false;
    // assign color and return
    _color = color;
    return true;
}

bool StatS::SRowProperty::setWidth(int width)
{
    // check if width is valid
    if (width < 1) return false;
    // check if new width is different from current
    if (_width == width) return false;
    // assign width and return
    _width = width;
    return true;
}

bool StatS::SRowProperty::setSize(int size)
{
    // check if size is valid
    if (size < 0) return false;
    // check if new size is different from current
    if (_size == size) return false;
    // assign size and return
    _size = size;
    return true;
}

// == inspectors ===============================================================

bool StatS::SRowProperty::isSelected() const
{
    return _selectedState;
}

// == accessor function definitions ============================================

QPixmap StatS::SRowProperty::getPixmap() const
{
    // create a 16x16 transparent pixmap..
    QPixmap pixmap = QPixmap(16,16);
    pixmap.fill(Qt::transparent);
    // ..and a painter connected to it
    QPainter p(&pixmap);
    p.setRenderHint(QPainter::Antialiasing);
    // set pen style and color based on SRowProperty object properties
    QPen pen = QPen();
    pen.setColor(_color);    
    pen.setWidth(_width - 1);
    if (_marker == Dot) pen.setWidth(_size + _width - 2);
    if (_size == 0) return pixmap;
    // bind pen to painter
    p.setPen(pen);
    // depending on the marker type, draw the proper pixmap
    switch (_marker) {
    case Dot: p.drawPoint(8, 8); break;
    case Circle:
        p.drawEllipse(7 - _size, 7 - _size,
                      2 * _size + 1, 2 * _size + 1);
        break;
    case Square:
        p.drawRect(7 - _size, 7 - _size,
                   2 * _size + 1, 2 * _size + 1);
        break;
    case Triangle:
        p.drawLine(8, 6 - _size / 2 ,
                   6 - _size / 2 , 11 + _size / 2 );
        p.drawLine(6 - _size / 2 , 11 + _size / 2 ,
                   11 + _size / 2 , 11 + _size / 2 );
        p.drawLine(11 + _size / 2 , 11 + _size / 2 ,
                   8, 6 - _size / 2 );
        break;
    default: qWarning() << "RowMarker not defined: " << _marker;
    }
    // return the pixmap
    return pixmap;
}

QPixmap StatS::SRowProperty::getColorPixmap()
{
    QPixmap pix(16, 16);
    QPainter p(&pix);
    p.fillRect(0, 0, 16, 16, _color);
    return pix;
}


// == static variables =========================================================

// == static funtion definitions ===============================================

// == private function definitions =============================================
